﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Project_1_refactored_
{
    class GameManager
    {
        #region declarations
        //GameInterface
        gameInterface GI = new gameInterface();

        Player pPlayer; //player which will be playing
        List<Enemy> eEnemies = new List<Enemy>(); // list of enemies in the game
        int iMaxEnemies = 1;

        int iScore = 0; //current score
        const int iScorePerKill = 14; //gained amount of points per kill

        //float fEnemyRespawnDelay = 0.15f; //how long wait till next enemy spawn
        //float fEnemyRespawnTime = 0.0f; //current time passed for respawnDelay

        //To make sure you can't take non stop dmg
        float fDamageTakenDelay = 0.50f;
        float fDamageTakenTime = 0.0f;
        #endregion

        #region constructor
        public GameManager(Player player)
        {
            pPlayer = player;
            enemiesInitialization();
        }
        #endregion

        #region LoadContent, Update, Draw methods
        public void LoadContent(ContentManager content)
        {
            pPlayer.LoadContent(content);

            foreach (Enemy enemy in eEnemies)
                enemy.LoadContent(content);

            //GI
            GI.LoadContent(content);
        }

        public void Update(GameTime gameTime)
        {
            collisionPvW(); //to check when player is killing zombies

            //DAMAGE TAKING DEALAY BEGIN
            fDamageTakenTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (fDamageTakenTime >= fDamageTakenDelay)
            {
                collisionPvE(); //enemy and player collision
                fDamageTakenTime = 0.0f;
            }
            //DAMAGE TAKING DEALAY END

            setInfoText(); //Updating score in the top left corner
            pPlayer.Update(gameTime);
            foreach (Enemy enemy in eEnemies)
                enemy.Update(gameTime);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Enemy enemy in eEnemies)
                enemy.Draw(spriteBatch);

            pPlayer.Draw(spriteBatch);
            //GI
            GI.Draw(spriteBatch);
        }
        #endregion

        #region helper Collision methods
        private void collisionPvE()
        {
            foreach(Enemy enemy in eEnemies)
                if(CollisionManager.intersect(pPlayer.collisionBOX(), enemy.collisionBOX()))
                {
                    if (enemy.Active())
                    {
                        //Player takes damage
                        pPlayer.applyMeleeDmg();
                    }
                }
        }

        private void collisionPvW()
        {
            foreach (Weapon weapon in pPlayer.pWeapons)
            {
                foreach (Enemy enemy in eEnemies)
                {
                    if (CollisionManager.intersect(weapon.collisionBOX(), enemy.collisionBOX()))
                    {
                        if (weapon.Active() && enemy.Active())
                        {
                            if (enemy.HP > 0)
                            {
                                pPlayer.removeWeapon(weapon);
                                enemy.applyRangeDmg();
                            }
                            if (enemy.HP <= 0)
                            {
                                enemy.deActivate();
                                pPlayer.removeWeapon(weapon);
                                iScore += iScorePerKill;
                            }
                            
                        }
                    }
                }
            }
        }
                
        #endregion

        #region helper methods
        private void enemiesInitialization()
        {
            Random rand = new Random();
            //creating a list of empty enemies right away so we could control how much there r in the current game
            for (int i = 0; i < iMaxEnemies; i++)
            {
                Enemy enemy = new Enemy();
                enemy.increaseLocation(i * 30, rand.Next(0,150)); //we increasing x location by i*44 and y by 0 rand.Next(0, 150)
                eEnemies.Add(enemy);
            }
        }

        private void setInfoText()
        {
            //PLAYER INFO
            string info = string.Format("PLAYER {2} STATISTICS: \nTOTAL SCORE: {0} \nTOTAL KILLS: {1} \n\nLEVEL: {3} \nMELEE ATTACK: {4} \nRANGE ATTACK: {5} \nDEFENCE: {6} \nHP: {7}",
                iScore.ToString(), (iScore/iScorePerKill).ToString(), pPlayer.NAME, pPlayer.LEVEL, pPlayer.MELEE_ATTACK, pPlayer.RANGE_ATTACK, pPlayer.DEFENCE, pPlayer.HP, eEnemies[0].HP);

            //ENEMY HP INFO
            string totalString = "";
            for(int i=0; i<eEnemies.Count; i++)
            {
                string enemyInfo = string.Format("\nENEMY_HP: {0} \nENEMY_STATE: {1}", eEnemies[i].HP, eEnemies[i].eSTATE);
                totalString += enemyInfo;
            }

            //PLAYER STATE && ENEMY STATE
            string pState = string.Format("\n\nPLAYER STATE = {0}",pPlayer.pSTATE);

            GI.setInfo(info+pState+totalString);
        }
        #endregion
    }
}
